﻿using CSRedis;
using EventBusETO.CustomerType;
using EventBusETO.RoomType;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Newtonsoft.Json;
using SH.Common.CommonReturn;

using SH.Order.Application.IServices;
using SH.Order.Application.IServices.Dto;
using SH.Order.Application.IServices.IOrder;
using SH.Order.Application.Services;
using SH.Order.Domain.OrderClass;
using SH.Order.ErrorCodeEnum;
using SH.Order.Infrastructure.IRepository;
using SH.Order.Infrastructure.Repository;
using SQLitePCL;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace SH.Order.Application.Service
{
    public class OrderServices : BaseServices<OrderInfo>, IOrderServices
    {
        private readonly IOrderRepository _orderRepository;
        private readonly IHttpClientFactory _httpclientFactory;
        private readonly IDicRepository _dicicRepository;
        private readonly CSRedisClient _redis = new CSRedisClient("10.31.59.3:6379,password=,defaultDatabase=0");
        public OrderServices(IOrderRepository orderRepository, IHttpClientFactory httpclientFactory, IDicRepository dicicRepository) : base(orderRepository)
        {
            _orderRepository = orderRepository;
            _httpclientFactory = httpclientFactory;
            _dicicRepository = dicicRepository;

        }
        private const string getAllCustomers = "http://10.31.59.14:9999/api/Customer/GetAllCustomers";
        private const string getRoomList = "http://10.31.59.3:9003/api/RoomType/GetRoomList";
        /// <summary>
        /// 订房订单显示
        /// </summary>
        /// <returns></returns>
        public ApiPaging<List<OrderInfoDto>> OrderInfoShow(DateTime? StartTime, DateTime? EndTime, int pageSize, int pageIndex, string? Name = "", int RTid = -1, int paymenttypesId = -1, int ordertypes = -1, int orderStatus = -1)
        {
            var query = _orderRepository.All;
            var diclist = _dicicRepository.GetDics();
            Func<OrderInfoDto, bool> predicate = x => true;

            predicate = x => (StartTime == null || x.EntryTime >= StartTime) && (EndTime == null || x.LeaveTime < EndTime.Value.AddDays(1))
            && (string.IsNullOrEmpty(Name) || x.OrderNo.Contains(Name) || x.Tel.Contains(Name) || x.CName.Contains(Name))
            && (RTid == -1 || x.Rtid == RTid) &&
            (paymenttypesId == -1 || x.PayMode == paymenttypesId)
           && (ordertypes == -1 || x.OrderType == ordertypes) && (orderStatus == -1 || x.OrderStatus == orderStatus) && (x.IsDelete == true)
           ;
            //客户
            var client = _httpclientFactory.CreateClient();
            var respone = client.GetAsync(getAllCustomers).Result;
            //房型
            var roomtype = _httpclientFactory.CreateClient();
            var roomresponse = roomtype.GetAsync(getRoomList).Result;
            if (roomresponse.IsSuccessStatusCode)
            {
                //客户
                var message = respone.Content.ReadAsStringAsync().Result;
                var customerlist = JsonConvert.DeserializeObject<List<CustomerEto>>(message);
                //房型
                var room = roomresponse.Content.ReadAsStringAsync().Result;
                var roomTypelist = JsonConvert.DeserializeObject<List<RoomTypeEto>>(room);
                var list = (from a in query.ToList()
                            join b in customerlist on a.CoustomerId equals b.CId
                            join c in roomTypelist on a.RTId equals c.Rtid
                            join d in diclist on a.OrderStatus equals d.Id
                            select new OrderInfoDto
                            {
                                OrderNo = a.OrderNo,
                                Rtname = c.Rtname,
                                CName = b.CName,
                                OrderType = a.OrderType,
                                Tel = b.Tel,
                                CId = b.CId,
                                EntryTime = a.EntryTime,
                                LeaveTime = a.LeaveTime,
                                ActualPay = c.Rprice,
                                OrderStatus = d.Id,
                                value = d.Value,
                                Id = a.Id,
                                OrderTime = a.OrderTime,
                                Rtid = c.Rtid,
                                IsDelete = a.IsDelete,
                                PayMode = a.PayMode,
                                RTId = c.Rtid,

                            }).ToList();
                int totalCount = 0;
                int pageCount = 0;
                list = list.Where(predicate).ToList();
                totalCount = list.Count;
                pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                ApiPaging<List<OrderInfoDto>> apiPaging = new ApiPaging<List<OrderInfoDto>>();
                apiPaging.Data = list;
                apiPaging.TotalCount = totalCount;
                apiPaging.PageCount = pageCount;
                return apiPaging;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete1(int id)
        {

            return _orderRepository.Delete1(id);
        }

        public List<OrderInfo> OrderShow()
        {
            var list = _orderRepository.GetList();
            return list;
        }

        public List<OrderInfo> OrderInfoApiShow()
        {
            var tenantId = _redis.Get<string>("TenantId");
            return _orderRepository.All.Where(x => x.TenantId == tenantId).ToList();
        }
    }
}
